package com.lai.note.controller.user;

import com.lai.note.dto.MessageBean;
import com.lai.note.model.User;
import com.lai.note.service.CopyWritingService;
import com.lai.note.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/user/copyWriting")
@Validated

public class UserCopyWirtingAction {

    @Autowired
    private CopyWritingService copyWritingService;

    @RequestMapping(value = "/addNew.action",method = RequestMethod.POST)
    public MessageBean userAddNew(HttpSession session, @NotNull(message = "文案内容不能为空") String content){
        User user = (User) session.getAttribute("user");
        if(copyWritingService.userAddNew(user.getUserId(),content))
            return MessageUtil.success();
        return MessageUtil.failure();
    }

    @RequestMapping(value = "/generate.action",method = RequestMethod.GET)
    public MessageBean RandomGenerate(){
        if(copyWritingService.randomGenerate()!=null){
            return MessageUtil.success(copyWritingService.randomGenerate().getContent());
        }else{
            return MessageUtil.failure("有bug");
        }
    }

    /**
     *  用户获取自己分享的文案
     * @return List<CopyWriting>
     */

    @RequestMapping(value = "/lookCopyWritingDetails.action",method = RequestMethod.GET)
    public MessageBean lookCopyWritingDetails(HttpSession session){
        User user = (User) session.getAttribute("user");
        return MessageUtil.success(copyWritingService.selectUserAllCopyWriting(user.getUserId()));
    }



}
